
AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')

include('shared.lua')


function ENT:Initialize()
	
	self.Entity:SetModel("models/weapons/w_hopwire.mdl")
	self.Entity:PhysicsInit(SOLID_BBOX)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_BBOX)
	self.Attached = false
	self.Thinking = false
	
	self.Entity:EmitSound("weapons/tripwire/ropeshoot.wav")
	
	local phys = self.Entity:GetPhysicsObject()
	
	if phys and phys:IsValid() then
		phys:Wake()
	end

end

function ENT:PhysicsCollide(data, physobj)

	if data.HitEntity:GetClass() == "tetherhook" then
	
		constraint.NoCollide(self.Entity, ent, 0, 0);
	
		return
	
	end

	if !self.Attached and data.HitEntity:IsWorld() then
		self.Attached = true
		
		self.Entity:SetMoveType(MOVETYPE_NONE)
		self.Entity:SetCollisionGroup(COLLISION_GROUP_NONE)

		self.Entity:StopSound("weapons/tripwire/ropeshoot.wav")

		/*
		
		local spring = ents.Create("phys_spring")
		spring:SetKeyValue("attach1", self.Entity:GetName())
		spring:SetKeyValue("attach2", self.Entity:GetOwner():GetName())
		spring:SetKeyValue("length", 32)
		spring:SetKeyValue("damping", 24)
		spring:SetKeyValue("relativedamping", 0.1)
		spring:SetKeyValue("constant", 150)
		spring:SetPos(self.Entity:GetPos())
		spring:Spawn()

		timer.Simple(0.5,
		function()
		
			if ValidEntity(spring) then
			
				spring:Remove();
			
			end
		
		end)
		
		*/
		
		self.Thinking = true
		self.Entity:NextThink(CurTime() + 0.1)
	end

end


function ENT:Think()
	
	if (self.Entity:GetNWBool("activated")) then
		local trace = {}
		trace.start = self.Entity:GetPos()
		trace.endpos = self.Entity:GetOwner():GetPos()
		trace.filter = {self.Entity, self.Entity:GetOwner()}
		trace.mask = MASK_SOLID
		
		local tr = util.TraceLine(trace)
		
		if (tr.Hit and tr.HitNonWorld and tr.Entity and tr.Entity:IsPlayer()) then
			self.Entity:GetOwner():GetTable():Explode()
		end
	end

end

function ENT:OnRemove()

	self.Entity:StopSound("weapons/tripwire/ropeshoot.wav")

end